package com.lu.util;

import com.lu.javabean.User;
import mapper.UserMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class UserTest {
    SqlSession session;
    UserMapper mapper;

    @Before
    public void a() {
        //1、创建SqlSessionFactory对象，也是单例模式的
        SqlSessionFactory factory = MyBatisUtil.getSqlSessionFactory();
        //2、创建SqlSession对象 true 自动提交事务 不写也没事 因为默认在sqlsession关闭时提交事务
        session = factory.openSession(true);
        //3、调用session的方法获取mapper
        mapper = session.getMapper(UserMapper.class);
    }

    @After
    public void b() {
        //5、关闭资源
        session.close();
    }

    @Test
    public void selectAllUser() {
        List<User> users = mapper.selectAllUser();
        //4、打印测试
        for (User u : users) {
            System.out.println(u);
        }
    }

    @Test
    public void selectById() {
        User user = mapper.selectById();
        System.out.println(user);
    }

    @Test
    public void insertUser() {
        User u = new User();
        u.setName("卢静文");
        u.setSal(666);
        u.setBirthday(new Date());
        mapper.insertUser(u);
    }
    @Test
    public void updateSalById() {
        int user = mapper.updateSalById(1,888);
        System.out.println(user);
    }

    @Test
    public void deleteByList() {
        List<Integer> ids = new ArrayList<>();
        ids.add(3);
        ids.add(4);

        mapper.deleteByList(ids);
    }
}
